<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>输入组件</title>
    <script src="../react/react.development.js"></script>
    <script src="../react/react-dom.development.js"></script>
    <script src="../react/babel.min.js"></script>
    <script src="../react/prop-types.js"></script>
    <style>
        textarea {
            width: 600px;
            height: 400px;
            font-size: 25px;
            font-weight: bold;
        }
    </style>
</head>
<body>
<div id="root"></div>
<button onclick="window.location.reload()">重新开始</button>
<script type="text/babel">
    class MyInput extends React.Component {
        constructor () {
            super()
            this.inputHandler = this.inputHandler.bind(this)
            this.inputSpeed = this.inputSpeed.bind(this)
            this.state = {
                value: '',
                count: 1,
                speed: 0
            }
        }

        inputHandler (e) {
            console.log('input')
            this.setState({
                value: e.target.value
            })
        }

        // 打字速度
        inputSpeed () {
            const time = 3000
            alert('计时开始')
            setInterval(() => {
                this.state.speed = this.state.value.length / (this.state.count * time / 1000)
                this.setState({
                    speed: Math.floor(this.state.speed * 60)
                })
                this.state.count++
            }, time)
        }

        render () {
            return (
                <div>
                    <textarea type="text" onInput={this.inputHandler}/>
                    <button onClick={this.inputSpeed}>开始打字</button>
                    <h2 style={this.props.h2}>内容长度：{this.state.value.length}</h2>
                    <h3 style={this.props.h2}>速度：{this.state.speed}字/分</h3>
                </div>
            )
        }
    }

    const h2 = {
        color: '#AA759F'
    }

    ReactDOM.render(<MyInput h2={h2}></MyInput>, document.getElementById('root'))
</script>
</body>
</html>
